// components/publishComment/index.js
import { Comment } from "../../model/Comment.js";
import { Upload } from "../../model/Upload.js";
const comment = new Comment();
const upload = new Upload();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    state: {
      type:Boolean,
      value:false
    },
    pid:{
      type:String,
      value:"0"
    },
    cid:{
      type:String,
      value:"0"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    commentCon: "",
    commentConLength: 0,
    tempFilePaths: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 绑定输入
     */
    bindInputComment(e) {
      let content = e.detail.value.replace(/\s+/g, "").replace(/[\r\n]/g, "");
      this.setData({
        commentCon: content,
        commentConLength: content.length
      });
    },
    /**
     * 上传图片
     */
    uploadPic() {
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (result) => {
          let files = result.tempFilePaths.map(file => {
            let objfile = {};
            objfile.type = 1;
            objfile.url = file;
            return objfile;
          });
          this.setData({
            sourceType: 1,
            tempFilePaths: files
          });
        },
        fail: (err) => {
          if (err.errMsg === "chooseImage:fail cancel") {
            wx.showToast({
              title: '取消选择',
              icon: "none"
            })
          } else {
            wx.showToast({
              title: '微信接口调用失败',
              icon: "none"
            })
          }
        }
      });
    },
    /**
     * 上传视频
     */
    uploadVideo() {
      wx.chooseVideo({
        sourceType: ['album', 'camera'],
        compressed: true,
        maxDuration: 20,
        success: (res) => {
          if (res.duration > 20) {
            wx.lin.showDialog({
              type: "alert",
              title: "提示",
              content: "视频长度不能大于20s",
              success: (res) => { }
            })
            return false;
          }
          let objfile = {
            type: 2,
            url: res.tempFilePath,
          }
          this.setData({
            sourceType: 2,
            tempFilePaths: [objfile]
          });
        },
        fail: (err) => {
          if (err.errMsg === "chooseImage:fail cancel") {
            wx.showToast({
              title: '取消选择',
              icon: "none"
            })
          } else {
            wx.showToast({
              title: '微信接口调用失败',
              icon: "none"
            })
          }
        }
      });
    },
    /**
     * 删除所选的资源
     */
    delPic(e) {
      this.setData({
        tempFilePaths: []
      });
    },
    /**
     * 发布评论
     */
    subComment() {
      if (this.data.commentCon == "" && this.data.tempFilePaths.length == 0) {
        wx.showToast({
          title: '请输入评论内容',
          icon: "none"
        })
        return false;
      }
      let options = {};
      if (this.data.cid == 0) {
        options = {
          postId: this.data.pid,
          content: this.data.commentCon,
          image: "",
          video: ""
        }
      } else {
        options = {
          commentId: this.data.cid,
          content: this.data.commentCon,
          image: "",
          video: ""
        }
      }
      if (this.data.tempFilePaths.length > 0) {
        upload.upload(this.data.tempFilePaths)
          .then(res => {
            let fileUrls = [];
            res.forEach((file) => {
              let fileData = JSON.parse(file.data);
              fileUrls.push(fileData.data.url);
            });
            if (this.data.sourceType == 1) {
              options.image = fileUrls.join(',');
            } else {
              options.video = fileUrls.join(',');
            }
            if (this.data.cid == 0) {
              this._publish(options);
            } else {
              this._replay(options);
            }

          })
          .catch(err => {
            wx.showToast({
              title: err.message,
              icon: "none"
            });
          });
      } else {
        if (this.data.cid == 0) {
          this._publish(options);
        } else {
          this._replay(options);
        }
      }
    },
    /**
     * 执行发布
     */
    _publish(options) {
      comment.comment(options)
        .then(res => {
          wx.showToast({
            title: '提交成功',
          });
          setTimeout(() => {
            this.setData({
              subCommentState: false,
              tempFilePaths: [],
              commentCon: "",
              commentConLength: 0
            })
            this.triggerEvent("reloadcomment",{
              ...options,
              newCommentId:res.data.CommentId,
              pid:this.data.pid,
              cid:this.data.cid
            });
          });
        })
        .catch(err => {
          wx.showToast({
            title: err.msg,
            icon: "none"
          })
        });
    },
    /**
     * 执行回复
     */
    _replay(options) {
      comment.reply(options)
        .then(res => {
          wx.showToast({
            title: '提交成功',
          });
          setTimeout(() => {
            this.setData({
              subCommentState: false,
              tempFilePaths: [],
              commentCon: "",
              commentConLength: 0
            })
            // this._getCommentList({
            //   type: "first",
            //   postId: this.data.id
            // });
          });
        })
        .catch(err => {
          wx.showToast({
            title: err.msg,
            icon: "none"
          });
        });
    }
  }
})
